package com.un.ebs.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.core.util.SecurityUtils;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.sys.domain.UserGroup;
import com.un.ebs.sys.domain.UserGroupItem;
import com.un.ebs.sys.domain.UserGroupItemExt;
import com.un.ebs.sys.mapper.UserGroupItemExtMapper;
import com.un.ebs.sys.mapper.UserGroupItemMapper;
import com.un.ebs.sys.mapper.UserGroupMapper;
import com.un.ebs.sys.service.UserGroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

//import com.un.ebs.sys.domain.UserGroupItem;
//import com.un.ebs.sys.domain.UserGroupItemExt;
//import com.un.ebs.sys.mapper.UserGroupItemExtMapper;
//import com.un.ebs.sys.mapper.UserGroupItemMapper;
//import com.un.ebs.sys.mapper.UserGroupMapper;
//import com.un.ebs.sys.domain.UserGroup;

/**
 * (UserGroup)表服务实现类
 *
 * @author liubo
 * @since 2020-08-23 11:52:17
 */
@Service
public class UserGroupServiceImpl extends ServiceImpl<UserGroupMapper, UserGroup> implements UserGroupService {

    @Resource
    private UserGroupMapper userGroupMapper;
    @Resource
    private UserGroupItemExtMapper itemExtMapper;
    @Resource
    private UserGroupItemMapper itemMapper;

    @Override
    public void update(String id, String name, String remark) {
        UserGroup obj;
        if (StringUtils.isNullOrEmpty(id)) {
            obj = new UserGroup();
            obj.setEntId(SecurityUtils.getUserContent().getEntId());
            obj.setTenantId(SecurityUtils.getTenantId());
        } else {
            obj = userGroupMapper.selectById(id);
        }
        obj.setName(name);
        obj.setRemark(remark);
        obj.insertOrUpdate();
    }

    @Override
    public void addUser(String groupId, String userId) {
        UserGroupItem userGroupItem = getUserGroupItem(groupId, userId);
        if (userGroupItem != null) {
            return;
        }
        UserGroupItem obj = new UserGroupItem();
        obj.setGroupId(groupId);
        obj.setUserId(userId);
        itemMapper.insert(obj);
    }

    @Override
    public void addUser(String groupId, List<String> userIds) {

    }

    private UserGroupItem getUserGroupItem(String groupId, String userId) {
        LambdaQueryWrapper<UserGroupItem> lq = Wrappers.lambdaQuery();
        lq.eq(UserGroupItem::getGroupId, groupId);
        lq.eq(UserGroupItem::getUserId, userId);
        return itemMapper.selectOne(lq);
    }

    @Override
    public void delete(String id) {
        IPage<UserGroupItemExt> iPage = queryItem("", id, 1, 1);
        if (iPage != null && iPage.getTotal() > 0) {
            throw new BusinessException("请先删除用户组别下的所有人员");
        }
        userGroupMapper.deleteById(id);
    }

    @Override
    public void deleteUser(String id) {
        itemMapper.deleteById(id);
    }

    @Override
    public void deleteUser(String groupId, String userId) {

    }

    @Override
    public IPage<UserGroup> query(String searchText, int pageSize, int pageIndex) {
        Page<UserGroup> page = new Page<>(pageIndex, pageSize);
        LambdaQueryWrapper<UserGroup> lq = Wrappers.lambdaQuery();
        lq.eq(UserGroup::getTenantId, SecurityUtils.getTenantId());
        if (!StringUtils.isNullOrEmpty(searchText)) {
            lq.and(obj -> obj
                    .like(UserGroup::getName, searchText).or()
                    .like(UserGroup::getRemark, searchText));
        }
        return userGroupMapper.selectPage(page, lq);
    }

    @Override
    public IPage<UserGroupItemExt> queryItem(String searchText, String groupId, int pageSize, int pageIndex) {
        Page<UserGroupItemExt> page = new Page<>(pageIndex, pageSize);
        LambdaQueryWrapper<UserGroupItemExt> lq = Wrappers.lambdaQuery();
        lq.eq(UserGroupItemExt::getGroupId, groupId);
        if (!StringUtils.isNullOrEmpty(searchText)) {
            lq.and(obj -> obj
                    .like(UserGroupItemExt::getTel, searchText).or()
                    .like(UserGroupItemExt::getPersonnelName, searchText));
        }
        return itemExtMapper.selectPage(page, lq);
    }

    @Override
    public UserGroup queryOrCreate(String name) {
        LambdaQueryWrapper<UserGroup> lq = Wrappers.lambdaQuery();
        lq.eq(UserGroup::getName, name);
        UserGroup userGroup = userGroupMapper.selectOne(lq);
        if (userGroup == null) {
            userGroup = new UserGroup();
            userGroup.setName(name);
            userGroup.setEntId(SecurityUtils.getUserContent().getEntId());
            userGroup.setTenantId(SecurityUtils.getTenantId());
            userGroup.insert();

            UserGroupItem item = new UserGroupItem();
            item.setGroupId(userGroup.getId());
            item.setUserId(SecurityUtils.getUserContent().getId());
            item.insert();
        }
        return userGroup;
    }

    @Override
    public List<UserGroup> queryByUserId(String userId) {
        LambdaQueryWrapper<UserGroupItem> lq = Wrappers.lambdaQuery();
        lq.eq(UserGroupItem::getUserId, userId);
        List<String> ids = itemMapper.selectList(lq).stream().map(s -> s.getGroupId()).distinct().collect(Collectors.toList());

        if (!StringUtils.isNullOrEmpty(ids)) {
            return userGroupMapper.selectBatchIds(ids);
        }
        return null;
    }

    @Override
    public List<String> queryUserGroupIdByUser(String userId) {
        List<UserGroup> list = queryByUserId(userId);
        if (StringUtils.isNullOrEmpty(list)) {
            return null;
        } else {
            return list.stream().map(s -> s.getId()).collect(Collectors.toList());
        }
    }
}
